﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Plat.WfProcess.Common;
using Plat.WfProcess.Business;
using Plat.WfProcess.DataModel;
using Plat.WfProcess.Utility;

namespace Plat.WfProcess.Engine
{
    /// <summary>
    /// 需要处理的任务类（其子类型有：人工任务或者自动任务）
    /// </summary>
    internal abstract class WorkItem : NodeBase
    {
        internal WorkItem(ActivityEntity activity)
            : base(activity)
        {

        }

        internal long TaskID
        {
            get;
            set;
        }

        internal bool IsAutomanticWorkItem
        {
            get
            {
                return Activity.IsAutomanticWorkItem;
            }
        }

        /// <summary>
        /// 创建任务的虚方法
        /// 1. 对于自动执行的工作项，无需重写该方法
        /// 2. 对于人工执行的工作项，需要重写该方法，插入待办的任务数据
        /// </summary>
        /// <param name="activityResource"></param>
        /// <param name="wfLinqDataContext"></param>
        internal virtual void CreateNewTask(ActivityResource activityResource,
            ProcessModelDataContext wfLinqDataContext)
        {

        }

        /// <summary>
        /// 完成任务的虚方法
        /// 1. 对于自动执行的工作项，无需重写该方法
        /// 2. 对于人工执行的工作项，需要重写该方法，设置任务为完成状态
        /// </summary>
        /// <param name="taskID"></param>
        /// <param name="activityResource"></param>
        /// <param name="wfLinqDataContext"></param>
        internal virtual void CompleteTask(long taskID,
            ActivityResource activityResource,
            ProcessModelDataContext wfLinqDataContext)
        {

        }

        /// <summary>
        /// 创建工作项及转移数据
        /// </summary>
        /// <param name="processInstance"></param>
        /// <param name="fromToTransition"></param>
        /// <param name="fromActivityInstance"></param>
        /// <param name="activityResource"></param>
        /// <param name="wfLinqDataContext"></param>
        internal void CreateNewWorkItemWithTransitionInstance(ProcessInstanceEntity processInstance,
            TransitionEntity fromToTransition,
            ActivityInstanceEntity fromActivityInstance,
            ActivityResource activityResource,
            ProcessModelDataContext wfLinqDataContext)
        {
            //实例化Activity
            base.CreateActivityInstanceObject(processInstance);

            //进入运行状态
            base.ActivityInstance.State = (short)NodeStateEnum.Running;

            //插入活动实例数据
            base.InsertActivityInstance(base.ActivityInstance,
                wfLinqDataContext);

            //插入任务数据
            CreateNewTask(activityResource, wfLinqDataContext);

            //插入转移数据
            base.InsertTransitionInstance(processInstance,
                fromToTransition,
                fromActivityInstance,
                base.ActivityInstance,
                wfLinqDataContext);
        }

        /// <summary>
        /// 完成任务实例
        /// </summary>
        /// <param name="taskID"></param>
        /// <param name="activityResource"></param>
        /// <param name="wfLinqDataContext"></param>
        internal void CompleteWorkItem(long taskID,
            ActivityResource activityResource,
            ProcessModelDataContext wfLinqDataContext)
        {
            //设置任务为完成状态
            CompleteTask(taskID, activityResource, wfLinqDataContext);
               
            //设置活动节点的状态为完成状态
            base.CompleteActivityInstance(base.ActivityInstance.ActivityInstanceGUID,
                activityResource,
                wfLinqDataContext);

            SyncActivityInstanceObjectState(NodeStateEnum.Completed);
        }
    }
}
